package com.ciwong.epaper.modules.me.ui.clazz;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DialogUtil;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.List;

/**
 * Created by neo on 2016/7/1.
 */
public class ChangeClassActivity extends BaseActivity{

    private int jumpType;
    private TextView tv_join_or_change_class_tips,tv_tips;
    private Button btn_confirm;
    private EditText edt_phone_num;
    private Clazz mClazz;
    private final int  CANCLE_TIPS=1;

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what){
                case CANCLE_TIPS:
                    tv_tips.setVisibility(View.GONE);
                    break;
            }
            return true;
        }
    });
    @Override
    protected void preCreate()
    {
        super.preCreate();
        jumpType = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_TYPE,
                IntentValue.JumpToJoinClassType.TYPE_JOIN_CLASS);
        if (IntentValue.JumpToJoinClassType.TYPE_CHANGE_CLASS == jumpType) {
            //用于更换班级的from class id
            mClazz = (Clazz) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
        }
    }

    @Override
    protected int setView() {
        return R.layout.activity_change_class;
    }

    @Override
    protected void findViews( ) {
        tv_join_or_change_class_tips = (TextView) findViewById(R.id.tv_join_or_change_class_tips);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        edt_phone_num = (EditText) findViewById(R.id.edt_phone_num);
        tv_tips = (TextView) findViewById(R.id.tv_tips);
    }


    @Override
    protected void initEvent() {
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edt_phone_num.getText() != null && edt_phone_num.getText().length() == 11) {
                    getNetClassListByMobile(edt_phone_num.getText().toString());
                }else {
//                    showDialog("请输入正确手机号" );
                    errTips(tv_tips, "请输入正确手机号" );
                }
            }
        });


        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        setResult(RESULT_CANCELED);
        finish();
    }

    @Override
    protected void init() {
        if (jumpType == IntentValue.JumpToJoinClassType.TYPE_JOIN_CLASS || jumpType == IntentValue.JumpToJoinClassType.FROM_CLASS_LIST)
        {// 加入类型
            setTitleText(getString(R.string.str_join_class));
            tv_join_or_change_class_tips.setText(getString(R.string.str_input_teacher_code_join_class_hint)+getString(R.string.str_join_class));
        }
        else
        {
            // 更换类型
            setTitleText(getString(R.string.str_change_class));
            tv_join_or_change_class_tips.setText(getString(R.string.str_input_teacher_code_join_class_hint)+getString(R.string.str_change_class));
        }

    }

    @Override
    protected void loadData() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case EpaperConstant.REQUEST_CODE_GET_CLASS_LIST:
                    setResult(RESULT_OK);
                    finish( );
                    break;
            }

        }
    }




    /**
     * 获取网络班级列表
     */
    private void getNetClassListByMobile(String mobileNum)
    {
        showCricleProgress();
        MeDao.getInstance().getClassListByTeacherPhoneNum(EApplication.BRAND_ID,mobileNum, new BaseExtCallBack(ChangeClassActivity.this, EApplication.getInstance().getUserInfoBase().getUserId()+"")
        {


            @Override
            public void success(Object data)
            {
                List<Clazz> clazzs = (List<Clazz>) data;
                hideMiddleProgressBar();

                hideCricleProgress();
                if (clazzs == null || clazzs.size() == 0) {
//                    showDialog( getString(R.string.str_no_relate_class) );
                    errTips(tv_tips, getString(R.string.str_no_relate_class) );
                }else {
                    MeJumpManager.jumpToClassList(ChangeClassActivity.this, R.string.go_back,jumpType,
                        EpaperConstant.REQUEST_CODE_GET_CLASS_LIST,edt_phone_num.getText().toString().trim(),mClazz);
                }

            }

            @Override
            public void failed(int errorCode, Object data)
            {
                super.failed(errorCode, data);
                hideCricleProgress();
                if(errorCode!=10004&&errorCode!=27&&errorCode!=17){
//                    showDialog( getString(R.string.str_no_relate_class) );
                    errTips(tv_tips, getString(R.string.str_no_relate_class) );
                }

//                DialogUtil.showFailedToastByErrorType(ChangeClassActivity.this,data);
            }

            @Override
            public void failed(Object data)
            {
                super.failed(data);
                hideCricleProgress();
//                showDialog( getString(R.string.str_no_relate_class) );
                errTips(tv_tips, getString(R.string.str_no_relate_class) );
//                DialogUtil.showFailedToastByErrorType(ChangeClassActivity.this,data);
            }
        });
    }


    private void showDialog( String  msg ) {
        DialogUtil.showTopHintDialog(this,msg,-1);
    }
    private void errTips(final TextView textView,String tips){
        Log.e(TAG, "errTips: " );
        textView.setText(tips);
        textView.setVisibility(View.VISIBLE);
//        tv_tips.scrollTo(0,0);
//        hideKeyboard(edt_phone_num);
        mHandler.sendEmptyMessageDelayed(CANCLE_TIPS,3000);
    }
    public static void hideKeyboard(View view){
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(),0);
        }
    }
}
